package com.atguigu.gulimall.member.service.impl;

import com.atguigu.common.exception.ErrorException;
import com.atguigu.common.vo.MemberInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.member.dao.MemberDao;
import com.atguigu.gulimall.member.entity.MemberEntity;
import com.atguigu.gulimall.member.service.MemberService;
import org.springframework.transaction.annotation.Transactional;


@Service("memberService")
@Transactional(rollbackFor = Exception.class)
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                new QueryWrapper<MemberEntity>()
        );

        return new PageUtils(page);
    }

    //检查手机号用户是否唯一
    @Override
    public boolean checkPhoneUsername(String phone, String username)  throws  ErrorException{
        Integer a = baseMapper.selectCount(new LambdaQueryWrapper<MemberEntity>().eq(MemberEntity::getMobile, phone));
        Integer b = baseMapper.selectCount(new LambdaQueryWrapper<MemberEntity>().eq(MemberEntity::getUsername, username));
        if (a>0){
           throw new ErrorException(10000,"电话号码存在");
        }
        if (b>0){
            throw new ErrorException(10001,"用户名存在");
        }

        return true;
    }

    @Override
    public MemberEntity findByPhone(String phone) {
        MemberEntity memberEntity = baseMapper.selectOne(new LambdaQueryWrapper<MemberEntity>().eq(MemberEntity::getMobile, phone));
        if (memberEntity==null){
            throw new ErrorException(10000,"账号不存在，请注册");
        }
        return memberEntity;
    }

    /**
     * 微博登录
     * @param vo
     * @return
     */
    @Override
    public MemberEntity oathuWeiBo(MemberInfoVo vo) {
        MemberEntity memberEntity = baseMapper.selectOne(new LambdaQueryWrapper<MemberEntity>().eq(MemberEntity::getWeiboId, vo.getId()));
        MemberEntity upd=new MemberEntity();
        //已注册
        if (memberEntity!=null){
            BeanUtils.copyProperties(memberEntity,upd);
            upd.setNickname(vo.getName());
            upd.setHeader(vo.getProfileImageUrl());
            upd.setGender(vo.getGender()=="m"?1:2);
            upd.setCity(vo.getLocation());
            baseMapper.updateById(upd);
            return  upd;
        }else {
            //未注册
            upd.setNickname(vo.getName());
            upd.setUsername(vo.getName());
            upd.setHeader(vo.getProfileImageUrl());
            upd.setGender(vo.getGender()=="m"?1:2);
            upd.setCity(vo.getLocation());
            upd.setWeiboId(vo.getId());
            upd.setLevelId(1L);
            baseMapper.insert(upd);
            return upd;

        }

    }


}